/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.entity.common;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.jrose.api.annotation.persistence.CreateTimestamp;
import org.jrose.api.annotation.persistence.PrimaryKey;
import org.jrose.api.annotation.persistence.Revision;
import org.jrose.api.annotation.persistence.UpdateTimestamp;

import java.io.Serializable;
import java.util.Date;

/**
 * Abstract entity which Long ID.
 *
 * @author Xiaohai Zhang
 * @since 2013-08-23 18:12
 */
@NoArgsConstructor(access = AccessLevel.PROTECTED)
abstract public class AbstractDatabaseEntity implements Serializable {
    private static final long serialVersionUID = -6642276954274614692L;

    @Getter @Setter @PrimaryKey(name = "ID", autoInc = true) private Long id;
    @Getter @Setter @CreateTimestamp(name = "CREATETIME") private Date createTime;
    @Getter @Setter @UpdateTimestamp(name = "UPDATETIME") private Date updateTime;
    @Getter @Setter @Revision(name = "REVISION") private Integer revision;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AbstractDatabaseEntity that = (AbstractDatabaseEntity) o;
        return new EqualsBuilder().append(id, that.id).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(13, 37).append(id).toHashCode();
    }

    @Override
    public String toString() {
        return "(" + getClass().getName() + ":" + id + ")";
    }
}
